package com.example.lvsuo.controller.cxg;

import cn.hutool.core.lang.Console;
import com.example.lvsuo.entity.cxg.vo.SystemLogVo;
import com.example.lvsuo.modal.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("redislogs")
public class Log1Controller {

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping
    public Result queryLog(){
        List<SystemLogVo> monitorAPI = redisTemplate.opsForList().range("monitorAPI", 0, -1);
        String nowDate= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        System.out.println("api为==>"+monitorAPI);


//        System.out.println("redis数据=="+monitorAPI);
        Map<Object, List<SystemLogVo>> grouping = monitorAPI.stream()
                .filter(slv -> slv.getStartDate().equals(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))))
                .collect(Collectors.groupingBy(slv -> slv.getApi()));

//        System.out.println("时间为=="+timeStr1);
//        grouping.forEach((api, SystemLogVo) -> System.out.println(api + "::" + grouping.size() ));
//        grouping.forEach(System.out::println);

        List<Map> list = new ArrayList();


        Iterator<Map.Entry<Object, List<SystemLogVo>>> iterator = grouping.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<Object, List<SystemLogVo>> next = iterator.next();
            String key = next.getKey().toString();
            List<SystemLogVo> value = next.getValue();
            Map map = new HashMap();
            map.put("name",key);
            map.put("value",value.size());
            list.add(map);
//            System.out.println(key + " " + value.size());
        }
//        System.out.println(list);

//        System.out.println(grouping);

        Map mapAll = new HashMap();

        mapAll.put("apiCount",list);
        mapAll.put("nowDate",nowDate);
        mapAll.put("apiAll",monitorAPI);

        return Result.back("ok","20000",mapAll);
    }


}
